package checkers;

import java.util.ArrayList;
import java.util.List;

public class CheckersMovement {

    private short id;
    private List<CheckersOperation> movementsList;

    public boolean containsAsSourceOrTarget(byte xPosition, byte yPosition) {
        for (CheckersOperation operation : this.movementsList) {
            if ((operation.operation == CheckersOperation.SOURCE_POSITION
                    || operation.operation == CheckersOperation.TARGET_POSITION)
                    && xPosition == operation.x && yPosition == operation.y) {
                return true;
            }
        }
        return false;
    }

    public boolean containsAsRemove(byte xPosition, byte yPosition) {
        for (CheckersOperation operation : this.movementsList) {
            if (operation.operation == CheckersOperation.REMOVE_PIECE
                    && xPosition == operation.x && yPosition == operation.y) {
                return true;
            }
        }
        return false;
    }

    public short getId() {
        return id;
    }

    public void setId(short id) {
        this.id = id;
    }

    public CheckersMovement(List<CheckersOperation> movementsList) {
        this.movementsList = movementsList;
    }

    public List<CheckersOperation> getMovementsList() {
        return movementsList;
    }

    public void setMovementsList(List<CheckersOperation> movementsList) {
        this.movementsList = movementsList;
    }

    @Override
    public CheckersMovement clone() {
        List<CheckersOperation> operationsList = new ArrayList<CheckersOperation>();
        for (CheckersOperation co : this.movementsList) {
            operationsList.add(co.clone());
        }
        CheckersMovement clone = new CheckersMovement(operationsList);
        clone.setId(this.id);
        return clone;
    }

    public CheckersOperation getLast(byte operation) {
        for (int i = this.movementsList.size() - 1; i >= 0; i--) {
            if (movementsList.get(i).operation == operation) {
                return movementsList.get(i);
            }
        }
        return null;
    }

    public CheckersOperation getFirst(byte operation) {
        for (int i = 0; i < this.movementsList.size(); i++) {
            if (movementsList.get(i).operation == operation) {
                return movementsList.get(i);
            }
        }
        return null;
    }
    
    public int getFirstIndex(byte operation) {
        for (int i = 0; i < this.movementsList.size(); i++) {
            if (movementsList.get(i).operation == operation) {
                return i;
            }
        }
        return -1;
    }

    public short countOperations(byte operation) {
        short i = 0;
        for (CheckersOperation co : movementsList) {
            if (co.operation == operation) {
                i++;
            }
        }
        return i;
    }

    boolean containsAsSourceOrTarget(int xPosition, int yPosition) {
        return containsAsSourceOrTarget(((byte) (xPosition)), ((byte) yPosition));
    }

    boolean containsAsRemove(int xPosition, int yPosition) {
        return containsAsRemove(((byte) (xPosition)), ((byte) yPosition));
    }
}
